package com.hbnu.tonghengchang;

import java.util.Arrays;

// 定义排序接口
interface Sorter {
    void sort(int[] array);
}

public class SorterDemo {
    // 冒泡排序的通用方法
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] arr1 = {5, 2, 9, 1, 3};
        int[] arr2 = {5, 2, 9, 1, 3};
        int[] arr3 = {5, 2, 9, 1, 3};

        // 1. 传统类实现
        class BubbleSorter implements Sorter {
            @Override
            public void sort(int[] array) {
                bubbleSort(array);
            }
        }
        Sorter sorter1 = new BubbleSorter();
        sorter1.sort(arr1);
        System.out.println("传统类实现: " + Arrays.toString(arr1));

        // 2. 匿名内部类实现
        Sorter sorter2 = new Sorter() {
            @Override
            public void sort(int[] array) {
                bubbleSort(array);
            }
        };
        sorter2.sort(arr2);
        System.out.println("匿名内部类实现: " + Arrays.toString(arr2));

        // 3. Lambda 表达式实现
        Sorter sorter3 = (array) -> bubbleSort(array);
        sorter3.sort(arr3);
        System.out.println("Lambda 表达式实现: " + Arrays.toString(arr3));
    }
}
